package org.ricks.log.pattern;

import org.ricks.log.LogEvent;

/**
 * @author chenwei
 * @Description:
 * @date 2023/1/916:55
 */
public abstract class AbstractPatternFormatter implements PatternFormatter {
    protected final FormattingInfo formattingInfo;
    private final boolean skipFormattingInfo;

    public AbstractPatternFormatter(FormattingInfo formattingInfo, String options) {
        this.formattingInfo = formattingInfo;
        this.skipFormattingInfo = FormattingInfo.getDefault().equals(formattingInfo);
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
//        if (skipFormattingInfo) {
            this.doFormat(event, toAppendTo);
//        } else {
//            this.formatWithInfo(event, toAppendTo);
//        }
    }

    private void formatWithInfo(final LogEvent event, final StringBuilder toAppendTo) {
        final int startField = toAppendTo.length();
        this.doFormat(event, toAppendTo);
        // 修正
        formattingInfo.format(startField, toAppendTo);
    }

    /**
     * 具体记录行为
     *
     * @param event      一次日志记录
     * @param toAppendTo 记录目标
     */
    protected abstract void doFormat(LogEvent event, StringBuilder toAppendTo);
}